package com.moore.utils;

import com.alibaba.fastjson2.JSONObject;

import java.util.List;

public class UserInfoUtils {
    public static String updateUserInfo(String email, String phone, String id) {
        String sql = "update userinfo set email = ? , phone = ? where user_id = ?";
        int num;
        try {
            num = JDBCUtils.executeUpdate(sql,email,phone,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (num > 0) {
            return "success";
        } else {
            return "fail";
        }
    }

    public static String insertUserInfo(String email, String phone, String id) {
        String sql = "insert into userinfo(email,phone,user_id) values(?,?,?)";
        int num;
        try {
            num = JDBCUtils.executeUpdate(sql,email,phone,id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (num > 0) {
            return "success";
        } else {
            return "fail";
        }
    }

    public static String getUserInfo(String id) {
        String sql = "select email,phone from userinfo where user_id = " + id;
        List<String> list;
        try {
            list = JDBCUtils.executeSelect(sql,"email","phone");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!list.isEmpty()) {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("email",list.get(0));
            jsonObject.put("phone",list.get(1));
            return jsonObject.toJSONString();
        } else {
            return "fail";
        }

    }
}
